package com.linktel.productionLine.controller;

import com.linktel.productionLine.common.BaseResponse;
import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.common.ResultUtils;
import com.linktel.productionLine.entity.dto.productYieldOffset.ProductYieldOffsetDTO;
import com.linktel.productionLine.entity.query.productionYieldOffset.AddProductYieldOffsetQuery;
import com.linktel.productionLine.entity.query.productionYieldOffset.EditProductYieldOffsetQuery;
import com.linktel.productionLine.exception.BusinessException;
import com.linktel.productionLine.service.ProductYieldOffsetService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 * @Author HuangJiaHui
 * @Date 2023/8/25 9:35
 */
@RestController
@RequestMapping("/product-yield-offset")
@AllArgsConstructor
public class ProductYieldOffsetController {

    private ProductYieldOffsetService productYieldOffsetService;

    /**
     * 查询所有产品良率补偿
     *
     * @return 产品良率补偿列表
     */
    @GetMapping
    public BaseResponse<List<ProductYieldOffsetDTO>> listProductYieldOffset() {
        return ResultUtils.success(productYieldOffsetService.listProductYieldOffset());
    }

    /**
     * 新增产品良率补偿
     *
     * @param addProductYieldOffsetQuery 新增的产品良率补偿封装对象
     * @return 是否新增成功
     */
    @PostMapping
    public BaseResponse<Boolean> addProductYieldOffset(@RequestBody AddProductYieldOffsetQuery addProductYieldOffsetQuery) {
        if (addProductYieldOffsetQuery == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(productYieldOffsetService.addProductYieldOffset(addProductYieldOffsetQuery));
    }

    /**
     * 删除产品良率补偿
     *
     * @param id 主键id
     * @return 是否删除成功
     */
    @DeleteMapping
    public BaseResponse<Boolean> deleteProductYieldOffset(Long currentUserId, Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(productYieldOffsetService.deleteProductYieldOffset(currentUserId, id));
    }

    /**
     * 修改产品良率补偿
     *
     * @param editProductYieldOffsetQuery 要修改的产品良率补偿
     * @return 是否更新成功
     */
    @PutMapping
    public BaseResponse<Boolean> editProductYieldOffset(@RequestBody EditProductYieldOffsetQuery editProductYieldOffsetQuery) {
        if (editProductYieldOffsetQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(productYieldOffsetService.editProductYieldOffset(editProductYieldOffsetQuery));
    }

}
